package com.luocj.brvah_srl;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.blankj.ALog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luocj.brvah_srl.activity.HomeActivity;
import com.luocj.brvah_srl.activity.IndexActivity;
import com.luocj.brvah_srl.activity.NestedScroActivity;
import com.luocj.brvah_srl.activity.SixthActivity;
import com.luocj.brvah_srl.activity.ThreeActivity;
import com.luocj.brvah_srl.adapter.BaseDecoration;
import com.luocj.brvah_srl.adapter.MyAdapter;
import com.luocj.brvah_srl.entity.MulEntity;
import com.luocj.brvah_srl.evnet.MyEvent;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private RecyclerView recyclerView;
    private SmartRefreshLayout smartFreshLayout;
    private ArrayList<MulEntity> multipeEntities;
    private MyAdapter adapter;
    public static final String[] titles = new String[]{"滑动嵌套", "微博"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        //透明状态栏
        StatusBarCompat.translucentStatusBar(this, true);
        EventBus.getDefault().register(this);
        initData();
        initView();
    }

    private ArrayList<MulEntity> initData() {
        multipeEntities = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            multipeEntities.add(new MulEntity(MulEntity.TEXT, 2));
            multipeEntities.add(new MulEntity(MulEntity.IMAGE, 2));
            multipeEntities.add(new MulEntity(MulEntity.TEXT_IMAGE, 4));
        }

        return multipeEntities;
    }

    private void initView() {
        smartFreshLayout = findViewById(R.id.smartrefreshlayout);
        recyclerView = findViewById(R.id.recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new MyAdapter(MainActivity.this, multipeEntities);
        adapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {

                return adapter.getData().get(position).getSpanSize();
            }
        });
        recyclerView.addItemDecoration(new BaseDecoration(Color.GRAY, 5));
        recyclerView.setAdapter(adapter);


        smartFreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (adapter.getData().size() != 0) {
                            adapter.getData().clear();
                        }
                        ArrayList<MulEntity> mulEntities = initData();
                        Log.i(TAG, "run: " + mulEntities.size());
                        adapter.addData(mulEntities);
                        smartFreshLayout.finishRefresh();
                        smartFreshLayout.setLoadmoreFinished(false);

                    }
                }, 2000);
            }
        });

        smartFreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<MulEntity> mulEntities = initData();
                        Log.i(TAG, "run: " + adapter.getData().size());
                        adapter.addData(mulEntities);
                        smartFreshLayout.finishLoadmore();
                    }
                }, 2000);
            }
        });

//        smartFreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
//            @Override
//            public void onLoadmore(final RefreshLayout refreshlayout) {
//                refreshlayout.getLayout().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (multipeEntities.size() != 0) {
//                            multipeEntities.clear();
//                        }
//
//                        ArrayList<MulEntity> mulEntities = initData();
//                        adapter.addData(mulEntities);
//                        smartFreshLayout.finishRefresh();
//                        smartFreshLayout.setLoadmoreFinished(false);
//
//                    }
//                }, 2000);
//            }
//
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.getLayout().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        ArrayList<MulEntity> mulEntities = initData();
//                        adapter.addData(mulEntities);
//                        smartFreshLayout.finishLoadmore();
//                    }
//                }, 2000);
//            }
//        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSyncSuccess(MyEvent event) {
        ALog.i(event.toString());
        if (event.getMsg().equals("0")) {
            startActivity(new Intent(MainActivity.this, NestedScroActivity.class));
        } else if (event.getMsg().equals("3")) {
            startActivity(new Intent(MainActivity.this, IndexActivity.class));
        } else if (event.getMsg().equals("6")) {
            startActivity(new Intent(MainActivity.this, SixthActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
